#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

#include "support.h"
#include "popupmenu.h"
#include "clist.h"
#include "newinfo.h"
#include "addgroup.h"

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
		g_object_set_data_full (G_OBJECT (component), name, \
		gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
		g_object_set_data (G_OBJECT (component), name, widget)

GtkWidget*
create_popup_menu_for_clist_show (void)
{
	GtkWidget *popup_menu_for_clist_show;
	GtkWidget *popup_item_open_link;
	GtkWidget *image_for_open_link;
	GtkWidget *popup_menu_separator_open;
	GtkWidget *popup_item_new_info;
	GtkWidget *image_for_new_info;
	GtkWidget *popup_menu_separator_new;
	GtkWidget *popup_item_delete_info;
	GtkWidget *image_for_delete_info;
	GtkWidget *popup_menu_separator_delete;
	GtkWidget *popup_item_copy_link;
	GtkWidget *image_for_copy_link;
	GtkWidget *popup_menu_separator_copy;
	
	GtkWidget *popup_item_copy_menu;
	GtkWidget *popup_item_copy_all;
	GtkWidget *image_copy_all;
	GtkWidget *separator_copy_all ;
	GtkWidget *popup_item_copy_blog ;
	GtkWidget *image_copy_blog;
	
	GtkWidget *popup_item_find_info;
	GtkWidget *image_for_find_info;
	 GtkWidget *popup_menu_separator_find;
	GtkWidget *popup_item_update_info;
	GtkWidget *image_for_update_info;

	popup_menu_for_clist_show = gtk_menu_new ();
	gtk_container_set_border_width (GTK_CONTAINER (popup_menu_for_clist_show), 2);

	popup_item_open_link = gtk_image_menu_item_new_with_mnemonic (_(
						"\346\211\223\345\274\200\351\223\276\346\216\245"));
	gtk_widget_show (popup_item_open_link);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_show),
											 popup_item_open_link);
	gtk_widget_set_size_request (popup_item_open_link, 100, -1);

	image_for_open_link = gtk_image_new_from_stock ("gtk-indent", GTK_ICON_SIZE_MENU);
	gtk_widget_show (image_for_open_link);
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (popup_item_open_link),
											 image_for_open_link);

	popup_menu_separator_open = gtk_separator_menu_item_new ();
	gtk_widget_show (popup_menu_separator_open);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_show),
											 popup_menu_separator_open);
	gtk_widget_set_sensitive (popup_menu_separator_open, FALSE);

	popup_item_new_info = gtk_image_menu_item_new_with_mnemonic (_(
						"\346\226\260\345\273\272\344\277\241\346\201\257"));
	gtk_widget_show (popup_item_new_info);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_show), popup_item_new_info);
	gtk_widget_set_size_request (popup_item_new_info, 100, -1);

	image_for_new_info = gtk_image_new_from_stock ("gtk-new", GTK_ICON_SIZE_MENU);
	gtk_widget_show (image_for_new_info);
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (popup_item_new_info), 
													image_for_new_info);

	popup_menu_separator_new = gtk_separator_menu_item_new ();
	gtk_widget_show (popup_menu_separator_new);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_show), 
											popup_menu_separator_new);
	gtk_widget_set_sensitive (popup_menu_separator_new, FALSE);

	popup_item_delete_info = gtk_image_menu_item_new_with_mnemonic (_(
						"\345\210\240\351\231\244\344\277\241\346\201\257"));
	gtk_widget_show (popup_item_delete_info);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_show), popup_item_delete_info);
	gtk_widget_set_size_request (popup_item_delete_info, 100, -1);

	image_for_delete_info = gtk_image_new_from_stock ("gtk-delete", GTK_ICON_SIZE_MENU);
	gtk_widget_show (image_for_delete_info);
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (popup_item_delete_info),
											 image_for_delete_info);

	popup_menu_separator_delete = gtk_separator_menu_item_new ();
	gtk_widget_show (popup_menu_separator_delete);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_show), 
											popup_menu_separator_delete);
	gtk_widget_set_sensitive (popup_menu_separator_delete, FALSE);

	popup_item_copy_link = gtk_image_menu_item_new_with_mnemonic (_(
						"\345\244\215\345\210\266\351\223\276\346\216\245"));
	gtk_widget_show (popup_item_copy_link);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_show), popup_item_copy_link);
	gtk_widget_set_size_request (popup_item_copy_link, 100, -1);

	image_for_copy_link = gtk_image_new_from_stock ("gtk-copy", GTK_ICON_SIZE_MENU);
	gtk_widget_show (image_for_copy_link);
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (popup_item_copy_link), 
													image_for_copy_link);
	/*-------------------------------------------------*/
	popup_item_copy_menu = gtk_menu_new ();
        gtk_menu_item_set_submenu (GTK_MENU_ITEM (popup_item_copy_link),popup_item_copy_menu);

        popup_item_copy_all = gtk_image_menu_item_new_with_mnemonic (_("  \345\244\215\345\210\266\346\211\200\346\234\211  "));
        gtk_widget_show (popup_item_copy_all);
        gtk_container_add (GTK_CONTAINER (popup_item_copy_menu), popup_item_copy_all);
       
        image_copy_all = gtk_image_new_from_stock ("gtk-copy", GTK_ICON_SIZE_MENU);
        gtk_widget_show (image_copy_all);
        gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (popup_item_copy_all), image_copy_all);
        
        separator_copy_all = gtk_separator_menu_item_new ();
        gtk_widget_show (separator_copy_all);
        gtk_container_add (GTK_CONTAINER (popup_item_copy_menu), separator_copy_all);
        gtk_widget_set_sensitive (separator_copy_all, FALSE);
        
        popup_item_copy_blog = gtk_image_menu_item_new_with_mnemonic (_("  \345\244\215\345\210\266\351\223\276\346\216\245  "));
        gtk_widget_show (popup_item_copy_blog);
        gtk_container_add (GTK_CONTAINER (popup_item_copy_menu), popup_item_copy_blog);
       
        image_copy_blog = gtk_image_new_from_stock ("gtk-network", GTK_ICON_SIZE_MENU);
        gtk_widget_show (image_copy_blog);
        gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (popup_item_copy_blog), image_copy_blog);
	/*--------------------------------------------------*/

	popup_menu_separator_copy = gtk_separator_menu_item_new ();
	gtk_widget_show (popup_menu_separator_copy);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_show), 
											popup_menu_separator_copy);
	gtk_widget_set_sensitive (popup_menu_separator_copy, FALSE);

	popup_item_find_info = gtk_image_menu_item_new_with_mnemonic (_(
						"\346\237\245\346\211\276\344\277\241\346\201\257"));
	gtk_widget_show (popup_item_find_info);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_show), popup_item_find_info);
	gtk_widget_set_size_request (popup_item_find_info, 100, -1);

	image_for_find_info = gtk_image_new_from_stock ("gtk-find", GTK_ICON_SIZE_MENU);
	gtk_widget_show (image_for_find_info);
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (popup_item_find_info),
													 image_for_find_info);
	/*------------------------------------------------------------------------------------*/
	popup_menu_separator_find = gtk_separator_menu_item_new ();
	gtk_widget_show (popup_menu_separator_find);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_show), 
											popup_menu_separator_find);
	gtk_widget_set_sensitive (popup_menu_separator_find, FALSE);

	popup_item_update_info = gtk_image_menu_item_new_with_mnemonic (_(
						"\344\277\256\346\224\271\344\277\241\346\201\257"));
	gtk_widget_show (popup_item_update_info);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_show), 
											popup_item_update_info);
	gtk_widget_set_size_request (popup_item_update_info, 100, -1);

	image_for_update_info = gtk_image_new_from_stock ("gtk-refresh", GTK_ICON_SIZE_MENU);
	gtk_widget_show (image_for_update_info);
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (popup_item_update_info),
											 image_for_update_info);
	/*------------------------------------------------------------------------------------*/
	g_signal_connect ((gpointer) popup_item_open_link, "activate",
							G_CALLBACK (on_popup_item_open_link_activate),
							(gpointer)&clist);
	g_signal_connect ((gpointer) popup_item_new_info, "activate",
							G_CALLBACK (on_popup_item_new_info_activate),
							NULL);
	g_signal_connect ((gpointer) popup_item_delete_info, "activate",
							G_CALLBACK (on_popup_item_delete_info_activate),
							(gpointer)&newinfo);
	g_signal_connect ((gpointer) popup_item_copy_all, "activate",
							G_CALLBACK (on_popup_item_copy_all_activate),
							(gpointer)&clist);
	g_signal_connect ((gpointer) popup_item_copy_blog, "activate",
							G_CALLBACK (on_popup_item_copy_blog_activate),
							(gpointer)&clist);
	g_signal_connect ((gpointer) popup_item_find_info, "activate",
							G_CALLBACK (on_popup_item_find_info_activate),
							NULL);
	g_signal_connect ((gpointer) popup_item_update_info, "activate",
							G_CALLBACK (on_popup_item_update_info_activate),
							(gpointer)&newinfo);
	/* Store pointers to all widgets, for use by lookup_widget(). */
	GLADE_HOOKUP_OBJECT_NO_REF (popup_menu_for_clist_show, popup_menu_for_clist_show, "popup_menu_for_clist_show");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_item_open_link, "popup_item_open_link");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, image_for_open_link, "image_for_open_link");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_menu_separator_open, "popup_menu_separator_open");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_item_new_info, "popup_item_new_info");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, image_for_new_info, "image_for_new_info");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_menu_separator_new, "popup_menu_separator_new");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_item_delete_info, "popup_item_delete_info");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, image_for_delete_info, "image_for_delete_info");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_menu_separator_delete, "popup_menu_separator_delete");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_item_copy_link, "popup_item_copy_link");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, image_for_copy_link, "image_for_copy_link");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_menu_separator_copy, "popup_menu_separator_copy");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_item_copy_menu,"popup_item_copy_menu ");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show,popup_item_copy_all ," popup_item_copy_all");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, image_copy_all," image_copy_all");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show,separator_copy_all ," separator_copy_all");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_item_copy_blog," popup_item_copy_blog");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show,image_copy_blog ," image_copy_blog");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_item_find_info, "popup_item_find_info");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, image_for_find_info, "image_for_find_info");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_menu_separator_copy, "popup_menu_separator_find");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, popup_item_find_info, "popup_item_update_info");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_show, image_for_find_info, "image_for_update_info");
	
	return popup_menu_for_clist_show;
}

GtkWidget*
create_popup_menu_for_clist_group (void)
{
	GtkWidget *popup_menu_for_clist_group;
	GtkWidget *popup_menu_for_add_group;
	GtkWidget *image_for_add_group;
	GtkWidget *popup_menu_separator_add;
	GtkWidget *popup_menu_for_delete_group;
	GtkWidget *image_for_delete_group;
	GtkWidget *popup_menu_separator_delete;
	GtkWidget *popup_menu_for_update_group;
	GtkWidget *image_for_update_group;

	popup_menu_for_clist_group = gtk_menu_new ();

	popup_menu_for_add_group = gtk_image_menu_item_new_with_mnemonic (_(
							"\346\267\273\345\212\240\345\210\206\347\273\204"));
	gtk_widget_show (popup_menu_for_add_group);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_group), popup_menu_for_add_group);
	gtk_widget_set_size_request (popup_menu_for_add_group, 100, -1);

	image_for_add_group = gtk_image_new_from_stock ("gtk-add", GTK_ICON_SIZE_MENU);
	gtk_widget_show (image_for_add_group);
	gtk_image_menu_item_set_image (
							GTK_IMAGE_MENU_ITEM (popup_menu_for_add_group),
							 image_for_add_group);

	popup_menu_separator_add = gtk_separator_menu_item_new ();
	gtk_widget_show (popup_menu_separator_add);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_group), popup_menu_separator_add);
	gtk_widget_set_sensitive (popup_menu_separator_add, FALSE);

	popup_menu_for_delete_group = gtk_image_menu_item_new_with_mnemonic (_(
							"\345\210\240\351\231\244\345\210\206\347\273\204"));
	gtk_widget_show (popup_menu_for_delete_group);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_group), popup_menu_for_delete_group);
	gtk_widget_set_size_request (popup_menu_for_delete_group, 100, -1);

	image_for_delete_group = gtk_image_new_from_stock ("gtk-clear", GTK_ICON_SIZE_MENU);
	gtk_widget_show (image_for_delete_group);
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (popup_menu_for_delete_group), image_for_delete_group);
	/*--------------------------------------------------------------------------------*/
	popup_menu_separator_delete = gtk_separator_menu_item_new ();
	gtk_widget_show (popup_menu_separator_delete);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_group), popup_menu_separator_delete);
	gtk_widget_set_sensitive (popup_menu_separator_delete, FALSE);

	popup_menu_for_update_group = gtk_image_menu_item_new_with_mnemonic (_("\344\277\256\346\224\271\345\210\206\347\273\204"));
	gtk_widget_show (popup_menu_for_update_group);
	gtk_container_add (GTK_CONTAINER (popup_menu_for_clist_group), popup_menu_for_update_group);
	gtk_widget_set_size_request (popup_menu_for_update_group, 100, -1);

	image_for_update_group = gtk_image_new_from_stock ("gtk-refresh", GTK_ICON_SIZE_MENU);
	gtk_widget_show (image_for_update_group);
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (popup_menu_for_update_group), image_for_update_group);
	/*----------------------------------------------------------------------------*/
	
	g_signal_connect ((gpointer) popup_menu_for_add_group, "activate",
										G_CALLBACK (on_popup_menu_for_add_group_activate),
										(gpointer)&addgroup);
	g_signal_connect ((gpointer) popup_menu_for_delete_group, "activate",
										G_CALLBACK (on_popup_menu_for_delete_group_activate),
										(gpointer)&newinfo);
		g_signal_connect ((gpointer) popup_menu_for_update_group, "activate",
										G_CALLBACK (on_popup_menu_for_update_group_activate),
										(gpointer)&addgroup);

	/* Store pointers to all widgets, for use by lookup_widget(). */
	GLADE_HOOKUP_OBJECT_NO_REF (popup_menu_for_clist_group, popup_menu_for_clist_group, "popup_menu_for_clist_group");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_group, popup_menu_for_add_group, "popup_menu_for_add_group");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_group, image_for_add_group, "image_for_add_group");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_group, popup_menu_separator_add, "popup_menu_separator_add");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_group, popup_menu_for_delete_group, "popup_menu_for_delete_group");
	GLADE_HOOKUP_OBJECT (popup_menu_for_clist_group, image_for_delete_group, "image_for_delete_group");

	return popup_menu_for_clist_group;
}
